package com.sinopec.reportfolder;


import java.util.Iterator;

import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.occa.infostore.CePropertyID;
import com.crystaldecisions.sdk.occa.infostore.IInfoObject;
import com.crystaldecisions.sdk.occa.infostore.IInfoObjects;
import com.crystaldecisions.sdk.occa.infostore.IInfoStore;
import com.crystaldecisions.sdk.occa.pluginmgr.IPluginInfo;
import com.crystaldecisions.sdk.occa.pluginmgr.IPluginMgr;
import com.crystaldecisions.sdk.plugin.desktop.folder.IFolders;
import com.crystaldecisions.sdk.plugin.desktop.folder.IFolder;

public class Folder {
	//get user Session
	public IInfoStore boInfoStore = null;
	private IPluginMgr boPluginMgr = null; 
	private IPluginInfo oPluginInfoNewFolder = null;
	private IInfoObjects oInfoObjects = null;
	
	private static IEnterpriseSession logon(String name,String pwd,String cms){
		IEnterpriseSession boEnterpriseSession = null;
		String boAuthType = "secEnterprise";
		try {
			boEnterpriseSession = CrystalEnterprise.getSessionMgr().
			logon(name,pwd,cms,boAuthType);
		}
		catch(SDKException e){
			System.err.println(e.getMessage());
		}
		return boEnterpriseSession;
	}
	public String add(int parentId,String title,IInfoStore boInfoStore){
		try {
			boPluginMgr = boInfoStore.getPluginMgr();
			oPluginInfoNewFolder = boPluginMgr.getPluginInfo("CrystalEnterprise.Folder");
			oInfoObjects = boInfoStore.newInfoObjectCollection();
			oInfoObjects.add(oPluginInfoNewFolder);
			IInfoObject oInfoObjectNewFolder = (IInfoObject)oInfoObjects.get(0);
			oInfoObjectNewFolder.setTitle(title);
			//oInfoObjectNewFolder.setDescription(folderDesc);
			//oInfoObjectNewFolder.setKeyword(folderKeywords);
			oInfoObjectNewFolder.properties().setProperty(CePropertyID.SI_PARENTID, parentId);
			boInfoStore.commit(oInfoObjects);
			
		} catch (SDKException e) {
			return e.getMessage();
		}
		return null;
	}
	/*
	 * @param name:001月报,002季报,003年报
	 * @
	 * */
	public IInfoObjects getParentFolders(String name,IInfoStore boInfoStore){
	//	IFolders parentFolders = null;
		IInfoObjects parentFolders = null;;
		try{
			parentFolders = (IInfoObjects)boInfoStore.query("SELECT SI_ID,SI_PATH FROM ci_infoobjects " +
				"WHERE si_kind='Folder' and SI_NAME='" + name + "'");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return parentFolders;
	}
	public static void main(String[] args){
		String system = args[0];			//localhost,10.1.19.141:6400
		String parentFolderName = args[1];	//001月报
		String addFolderName = args[2];		//201107
		
		IEnterpriseSession boEnterpriseSession = logon("administrator","sinopechrbo00",system);
		//IFolders parentFolders = null;
		IInfoObjects parentFolders;
		if (null == boEnterpriseSession){
			System.err.println("logon failed!");
		}
		else{
			Folder boFolder = new Folder();
			try {
				boFolder.boInfoStore = (IInfoStore)boEnterpriseSession.getService("","InfoStore");
				parentFolders = boFolder.getParentFolders(parentFolderName,boFolder.boInfoStore);
				int folderNum = 0;
				int parentId = -1;
				Iterator folder =(Iterator) parentFolders.iterator();
				String msg;
				while (folder.hasNext()){
					parentId = ((IInfoObject)folder.next()).getID();
					System.out.println("parentId is : " + parentId + " count: " + folderNum);
					msg = boFolder.add(parentId, addFolderName, boFolder.boInfoStore);
					if (msg != null){
						System.err.println(msg);
					}
					//System.out.println("parentId is :" + parentId + "count: " + folderNum);
					folderNum++;
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally
		    {	if (null != boEnterpriseSession){
		    		boEnterpriseSession.logoff();
		    	}
		    }
			System.out.println("folder name is: " + addFolderName);
			System.out.println("folder have been succefully created!");
		}
		
	}
}
